define(function (require, exports) {
    exports.setFromPath = function (json, name, value) {
        var path = name.split(".");

        // 依次创建不存在的值
        var key = "json";
        for (var i = 0; i < path.length; i++) {
            key = key + "." + path[i];
            var oldValue = null;
            var newValue = null;

            try {
                oldValue = eval(key);
            } catch (e) {
            }

            if (!oldValue) {
                // 叶结点直接赋值
                if (i == path.length - 1) {
                    newValue = value;
                    eval(key + "=newValue");
                } else {
                    // 父结点初始化值
                    // 数组
                    if (path[i].indexOf("[") > 0) {
                        var oldArray = eval(key.substring(0, key.lastIndexOf("[")));

                        if (!oldArray) {
                            oldArray = [
                                {}
                            ];
                        }

                        var re = /\[(\d+)\]/g;

                        if (re.test(path[i])) {
                            var index = parseInt(RegExp.$1);
                            for (var k = 0; k < index - (oldArray.length - 1); k++) {
                                oldArray.push({});
                            }
                        }

                        newValue = oldArray;
                        eval(key.substring(0, key.lastIndexOf("[")) + "=newValue");
                    } else {
                        // json
                        newValue = {};
                        eval(key + "=newValue");
                    }
                }
            }
        }

        return json;
    };

    exports.getFromPath = function (json, path) {
        try {
            return eval("json." + path);
        }
        catch (e) {
        }

        return null;
    };
});